package lexer;

import java.io.*;
import java.util.ArrayList;

import miniJava.Grammar;
import miniJava.ParseException;
import miniJava.Token;


public class Lexer {
        
        private Grammar g;   
        private ArrayList<Token> token; 
        
        public Lexer(InputStream ins){
                g = new Grammar(ins);
                token = new ArrayList<Token>();
        }
        
        public InputStream lexicalAnalysis(ArrayList<String> lexicalError) throws ParseException, Exception{
                Token t = new Token();
                String le = "";
                do{
                        t = g.token;
                        
                        if(t.kind == 43){
                                le = "Lexical error at line " + 
                                t.beginLine + ", column " +
                                t.endColumn + ". Encountered: " + t.image;
                                lexicalError.add(le);
                        }else{
                                token.add(t);
                                le = le + " " + t.image;
                        }
                }while(t.kind != 0);
				return new ByteArrayInputStream(le.getBytes("UTF-8"));
        }
        public void showTokens() throws ParseException{
        		
                for(int i = 0; i < token.size(); i++){
                    System.out.println(token.get(i).image);
                }
        }       
}